package main

import (
	"context"
	"encoding/json"
	"google.golang.org/grpc"
	"google.golang.org/grpc/status"
	"inventory/api/inventory"
	"log"
)

func main() {
	//ch := make(chan int)
	//
	//go func() {
	//	v := <-ch
	//	fmt.Println(v)
	//}()
	//ch <- 1
	//fmt.Println("2")
	conn, e := grpc.Dial("127.0.0.1:3456", grpc.WithInsecure(), grpc.WithBlock())
	if e != nil {
		log.Fatalln(e)
	}
	defer func() {
		_ = conn.Close()
	}()
	client := inventory.NewInventoryClient(conn)
	ctx := context.Background()
	if resp0, err := client.CalculateDeliverFee(ctx, &inventory.CalculateDeliverFeeRequest{
		RequestId: "20210528173731010000000082",
		Lat:       30.281388,
		Lng:       120.017384,
		CityCode:  "156330110",
		Packages: []*inventory.Package{{
			Id:          39,
			DeliverType: 2,
			Weight:      3020,
			TotalAmount: 6,
			ExpectFee:   0,
			Skipped:     false,
		}},
		Source: 2,
		SgId:   43,
	}); err != nil {
		for _, detail := range status.Convert(err).Details() {
			switch t := detail.(type) {
			case *inventory.DefaultResp:
				log.Println(t.ErrorMessage)
			}
		}
	} else {
		data, _ := json.Marshal(resp0)
		log.Println(string(data))
	}
}
